package com.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.GradeBean;
import com.project.bean.RoleBean;
import com.project.mapper.IGradeMapper;
import com.project.mapper.IRoleMapper;
import com.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.relation.Role;
import java.util.List;
@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleMapper roleMapper;

    @Autowired
    private IGradeMapper gradeMapper;



    @Override
    public List<GradeBean> findByRoleArr(Integer[] roles) {
        return gradeMapper.findByRoleArr(roles);
    }

    @Override
    public IPage<RoleBean> cutAll(Integer pageNO ) {
        return   roleMapper.selectPage(new  Page(pageNO,3),null );

    }

    @Override
    public List<RoleBean> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public void add(RoleBean roleBean, Integer[] gradeArr) {
        roleMapper.insert(roleBean);
        roleMapper.addList(roleBean.getId(),gradeArr);
    }

    @Override
    public void del(Integer roleId) {
        roleMapper.delUserRoleOne(roleId);
        roleMapper.delRoleGrades(roleId);
        roleMapper.deleteById(roleId);
    }

    @Override
    public RoleBean findById(Integer roleId) {
        RoleBean roleBean= roleMapper.selectById(roleId);
        roleBean.setGradeBeans(gradeMapper.findByRoleId(roleId));
        return roleBean;
    }

    @Override
    public void update(Integer roleId, Integer[] gradeIds) {
        roleMapper.delRoleGrades(roleId);
        roleMapper.addList(roleId,gradeIds);
    }
}
